package com.zz.hhbl.utils

import android.content.DialogInterface
import android.view.KeyEvent
import androidx.fragment.app.FragmentActivity
import com.zz.framework.core.ext.toLanguage
import com.zz.framework.core.utils.language.LanguageManager
import com.zz.framework.core.utils.language.Strings
import com.zz.framework.core.xdialog.XDialog
import com.zz.framework.core.xdialog.listener.OnBindViewListener
import com.zz.hhbl.R
import com.zz.hhbl.databinding.DialogOneButtonBinding
import com.zz.hhbl.databinding.DialogTwoButtonBinding
import com.zz.hhbl.databinding.DialogVersionUpdateButtonBinding

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/28 16:50.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
object DialogUtils {
    fun showTwoButtonDialog(activity: FragmentActivity, title: String, content: String, leftText: String = Strings.取消, leftListener: () -> Unit={},  rightText: String =Strings.确定,rightListener: () -> Unit = {},) {
        XDialog.Builder(activity.supportFragmentManager)
            .setViewBinding(DialogTwoButtonBinding::class.java)
            .setScreenWidthAspect(activity, 0.7f)
            .setDialogAnimationRes(R.style.animate_dialog_scale)
            .setOnBindViewListener(object : OnBindViewListener<DialogTwoButtonBinding> {
                override fun onBind(binding: DialogTwoButtonBinding, xDialog: XDialog) {
                    binding.apply {
                        tvTitle.text = title.toLanguage()
                        tvContent.text = content.toLanguage()
                        tvCancel.text = leftText.toLanguage()
                        tvCancel.setOnClickListener {
                            xDialog.dismiss()
                            leftListener.invoke()
                        }
                        tvConfirm.text = rightText.toLanguage()
                        tvConfirm.setOnClickListener {
                            xDialog.dismiss()
                            rightListener.invoke()
                        }
                    }
                }
            })
            .setCancelableOutside(false)
            .create()
            .show();
    }
    fun showVersionUpdateDialog(activity: FragmentActivity, leftText: String = Strings.取消, leftListener: () -> Unit={},  rightText: String =Strings.确定,rightListener: () -> Unit = {},): XDialog {
        return XDialog.Builder(activity.supportFragmentManager)
            .setViewBinding(DialogVersionUpdateButtonBinding::class.java)
            .setScreenWidthAspect(activity,0.717f)
            .setDialogAnimationRes(R.style.animate_dialog_scale)
            .setOnBindViewListener(object : OnBindViewListener<DialogVersionUpdateButtonBinding> {
                override fun onBind(binding: DialogVersionUpdateButtonBinding, xDialog: XDialog) {
                    binding.apply {
                        constraintLayout.setBackgroundResource(if (LanguageManager.currentLangCode == "en") R.mipmap.ic_version_update_dialog_bg_en else R.mipmap.ic_version_update_dialog_bg)
                        btnCancel.text = leftText.toLanguage()
                        btnCancel.setOnClickListener {
                            xDialog.dismiss()
                            leftListener.invoke()
                        }
                        btnConfirm.text = rightText.toLanguage()
                        btnConfirm.setOnClickListener {
                            xDialog.dismiss()
                            rightListener.invoke()
                        }
                    }
                }
            })
            .setCancelableOutside(false)
            .create()
            .show();
    }

    fun showOneButtonDialog(activity: FragmentActivity, title: String, content: String,isCancelable:Boolean = true,buttonText: String, buttonListener: () -> Unit = {},) {
        XDialog.Builder(activity.supportFragmentManager)
            .setViewBinding(DialogOneButtonBinding::class.java)
            .setScreenWidthAspect(activity, 0.7f)
            .setCancelableOutside(isCancelable)
            .setDialogAnimationRes(R.style.animate_dialog_scale)
            .setOnBindViewListener(object : OnBindViewListener<DialogOneButtonBinding> {
                override fun onBind(binding: DialogOneButtonBinding, xDialog: XDialog) {
                    binding.apply {
                        tvTitle.text = title.toLanguage()
                        tvContent.text = content.toLanguage()
                        tvConfirm.text = buttonText.toLanguage()
                        tvConfirm.setOnClickListener {
                            buttonListener.invoke()
                            xDialog.dismiss()
                        }
                    }
                }
            })
            .setOnKeyListener(DialogInterface.OnKeyListener { dialog, keyCode, event ->
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return@OnKeyListener isCancelable.not()
                }
                false
            })
            .create()
            .show()
    }
}